package com.cdtye.itps.cms.service.datasysn;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cdtye.common.mybatiscore.base.BaseService;
import com.cdtye.itps.cms.dao.detect.JcjcDefectTypeSection;
import com.cdtye.itps.cms.dao.enums.DelFlagEnum;
import com.cdtye.itps.cms.mapper.datasysn.JcjcDefectTypeMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class JcwDefectTypeService extends BaseService<JcjcDefectTypeMapper, JcjcDefectTypeSection> {
    @Autowired
    JcjcDefectTypeMapper jcjcDefectTypeMapper;

    @DS("gz")
    public JcjcDefectTypeSection findDefectTypeByCodeGZ(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }
    @DS("sz")
    public JcjcDefectTypeSection findDefectTypeByCodeSZ(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }
    @DS("cs")
    public JcjcDefectTypeSection findDefectTypeByCodeCS(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }
    @DS("hy")
    public JcjcDefectTypeSection findDefectTypeByCodeHY(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }
    @DS("hh")
    public JcjcDefectTypeSection findDefectTypeByCodeHH(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }
    @DS("hk")
    public JcjcDefectTypeSection findDefectTypeByCodeHK(String defectTypeCode, String parentId){
        return this.findDefectTypeByCodeXX(defectTypeCode, parentId);
    }

    public JcjcDefectTypeSection findDefectTypeByCodeXX(String defectTypeCode, String parentId){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("defect_type_code",defectTypeCode);
        queryWrapper.ne("del_flag","Y");
        if (StringUtils.isNotBlank(parentId)) {
            queryWrapper.eq("parent_id",parentId);
        }
        List<JcjcDefectTypeSection> ljdt =jcjcDefectTypeMapper.selectList(queryWrapper);
        if (ljdt.size()>0){
            return ljdt.get(0);
        }
        return null;
    }


    @DS("gz")
    public JcjcDefectTypeSection findDefectTypeByNameGZ(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }
    @DS("sz")
    public JcjcDefectTypeSection findDefectTypeByNameSZ(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }
    @DS("cs")
    public JcjcDefectTypeSection findDefectTypeByNameCS(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }
    @DS("hy")
    public JcjcDefectTypeSection findDefectTypeByNameHY(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }
    @DS("hh")
    public JcjcDefectTypeSection findDefectTypeByNameHH(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }
    @DS("hk")
    public JcjcDefectTypeSection findDefectTypeByNameHK(String defectTypeName, String parentId){
        return this.findDefectTypeByNameXX(defectTypeName, parentId);
    }

    public JcjcDefectTypeSection findDefectTypeByNameXX(String defectTypeName, String parentId){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("defect_type_name",defectTypeName);
        queryWrapper.and(wrapper  -> wrapper.eq("DEL_FLAG", DelFlagEnum.NO).or().isNull("DEL_FLAG"));
        if (StringUtils.isNotBlank(parentId)) {
            queryWrapper.eq("parent_id",parentId);
        }
        List<JcjcDefectTypeSection> ljdt =jcjcDefectTypeMapper.selectList(queryWrapper);
        if (ljdt.size()>0){
            return ljdt.get(0);
        }
        return null;
    }


    @DS("gz")
    public List<JcjcDefectTypeSection> findCategoryDateGZ(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
//                wrapper.isNull("update_datetime").or().lt("update_datetime",date).isNull("parent_id").or().ne("parent_id", "4226")).orderByAsc("TO_NUMBER(SORT_NUM)");
                wrapper.isNull("parent_id"));
//        return this.page(new Page<>(1, 10000),queryWrapper).getRecords();
        List<JcjcDefectTypeSection> datas = jcjcDefectTypeMapper.selectList(queryWrapper);

        queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.eq("parent_id", "4226"));
        List<JcjcDefectTypeSection> data2 = jcjcDefectTypeMapper.selectList(queryWrapper);
        datas.addAll(data2);

        queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
//                wrapper.isNull("update_datetime").or().lt("update_datetime",date).isNull("parent_id").or().ne("parent_id", "4226")).orderByAsc("TO_NUMBER(SORT_NUM)");
                wrapper.ne("parent_id", "4226")).orderByAsc("TO_NUMBER(SORT_NUM)");
        List<JcjcDefectTypeSection> datas3 = jcjcDefectTypeMapper.selectList(queryWrapper);
        datas.addAll(datas3);
        return datas;
    }
    @DS("cs")
    public List<JcjcDefectTypeSection> findCategoryDateCS(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date)).orderByDesc("PARENT_ID");
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }
    @DS("sz")
    public List<JcjcDefectTypeSection> findCategoryDateSZ(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date)).orderByDesc("PARENT_ID");
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }
    @DS("hh")
    public List<JcjcDefectTypeSection> findCategoryDateHH(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date)).orderByDesc("PARENT_ID");
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }
    @DS("hy")
    public List<JcjcDefectTypeSection> findCategoryDateHY(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date)).orderByDesc("PARENT_ID");
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }
    @DS("hk")
    public List<JcjcDefectTypeSection> findCategoryDateHK(Date date){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("del_flag","Y");
        queryWrapper.and(wrapper  ->
                wrapper.isNull("update_datetime").or().lt("update_datetime",date)).orderByDesc("PARENT_ID");
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }


    @DS("tmis")
    public List<JcjcDefectTypeSection> findDefectTypeByParentIdTmis(String parentId){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.ne("del_flag","Y");
        List<JcjcDefectTypeSection> ljdt =jcjcDefectTypeMapper.selectList(queryWrapper);
        return ljdt;
    }

    @DS("tmis")
    public List<JcjcDefectTypeSection> findDefectTypeListByParamsTmis(String defectTypeName, String parentId){
        QueryWrapper<JcjcDefectTypeSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(wrapper  -> wrapper.eq("DEL_FLAG", DelFlagEnum.NO.value).or().isNull("DEL_FLAG"));
        if (StringUtils.isNotBlank(defectTypeName)) {
            queryWrapper.eq("defect_type_name",defectTypeName);
        }
        if (StringUtils.isNotBlank(parentId)) {
            queryWrapper.eq("parent_id",parentId);
        }
        return jcjcDefectTypeMapper.selectList(queryWrapper);
    }
}
